package com.xunkids.bos.migration.business.controller.device;


import com.aliyuncs.exceptions.ClientException;
import com.xunkids.bos.migration.common.constant.BusinessConstant;
import com.xunkids.bos.migration.common.exception.RushException;
import com.xunkids.bos.migration.component.oss.OSStorage;
import com.xunkids.bos.migration.component.transmission.api.TransportApi;
import com.xunkids.bos.migration.component.transmission.pojo.RequestMessage;
import com.xunkids.bos.migration.dto.bos.entity.BosEventBaseDto;
import com.xunkids.bos.migration.dto.bos.entity.BosEventTestDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @program: bos-data-migration
 * @author: byl
 * @create: 2024-03-28 16:38
 * @Description
 */
@Api(tags = "BOS双写")
@RestController
@RequestMapping("/bos")
public class BwDeviceEPController {

    private final TransportApi transportApi;


    public BwDeviceEPController(TransportApi transportApi) {
        this.transportApi = transportApi;
    }

    @GetMapping("/test")
    public void test(){
        System.out.println("test");
    }


    @ApiOperation("操作其他的bucket")
    @PostMapping("/AddOrDelBosFileForOtherBucket")
    public void addOrDelBosFileForOtherBucket(@RequestBody BosEventBaseDto dto) throws RushException {
         transportApi.sendMessage(new RequestMessage(dto.getEvents().get(0).getEventId(), BusinessConstant.TRANSPORT_REQUEST_CODE,BusinessConstant.TRANSPORT_TOPIC_OTHER,dto));
    }

    @ApiOperation("操作BBWATCH的bucket")
    @PostMapping("/AddOrDelBosFileForBBWATCHBucket")
    public void addOrDelBosFileForBbWatchBucket(@RequestBody BosEventBaseDto dto) throws RushException {
        transportApi.sendMessage(new RequestMessage(dto.getEvents().get(0).getEventId(), BusinessConstant.TRANSPORT_REQUEST_CODE,BusinessConstant.TRANSPORT_TOPIC_BBWATCH,dto));
    }

    @ApiOperation("在阿里OSS上面创建bucket")
    @PostMapping("/creatBucket/{bucketName}")
    public String creatBucket(@PathVariable String bucketName) throws ClientException {
        return OSStorage.getInstance().buildBucket(BusinessConstant.XIAOMI_BUCKET_PREFIX+ bucketName +BusinessConstant.XIAOMI_BUCKET_SUFFIX);
    }







    @ApiOperation("在阿里OSS上面传入对象")
    @PostMapping("/putObject}")
    public void putObject(@RequestParam("bucketName") String bucketName,@RequestParam("objectKey") String objectKey ,@RequestPart("file") MultipartFile file) throws ClientException, IOException {
        InputStream inputStream = file.getInputStream();
        OSStorage.getInstance().putObject(bucketName,objectKey,inputStream);
    }










}
